package com.mine.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mine.pojo.MineMallEvaluate;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.service.EvaluateService;

@Controller
public class EvaluateController {

	@Autowired
	EvaluateService evaluateService;
	
	//用户对商铺里的某一件商品评价
	@RequestMapping(value="/evaluate/goods/save/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result createEvaluateToGoods(@PathVariable String token, @RequestBody MineMallEvaluate mallEvaluate){
		
		if (mallEvaluate==null||mallEvaluate.getGoodsId()==null||mallEvaluate.getContent()==null||mallEvaluate.getScore()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		mallEvaluate.setType((byte)0);
		mallEvaluate.setStatus((byte)1);
		Result result = evaluateService.saveEvaluateToGoods(token, mallEvaluate);
		
		return result;
	}
	//用户修改对商铺里的某一件商品评价
	@RequestMapping(value="/evaluate/goods/update/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updateEvaluateToGoods(@PathVariable String token, @RequestBody MineMallEvaluate mallEvaluate){
		
		if (mallEvaluate==null||mallEvaluate.getGoodsId()==null||mallEvaluate.getContent()==null||mallEvaluate.getScore()==null||mallEvaluate.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		mallEvaluate.setType((byte)0);
		mallEvaluate.setStatus((byte)1);
		Result result = evaluateService.updateEvaluateToGoods(token, mallEvaluate);
		
		return result;
	}
	
	//用户对商铺的评价
	@RequestMapping(value="/evaluate/shop/save/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result createEvaluateToShop(@PathVariable String token, @RequestBody MineMallEvaluate mallEvaluate){
		
		if (mallEvaluate==null||mallEvaluate.getContent()==null||mallEvaluate.getScore()==null||mallEvaluate.getShopId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		mallEvaluate.setGoodsId(null);
		mallEvaluate.setType((byte)1);
		mallEvaluate.setStatus((byte)1);
		Result result = evaluateService.saveEvaluateToShop(token, mallEvaluate);
		
		return result;
	}
	//用户修改对商铺的评价
	@RequestMapping(value="/evaluate/shop/update/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updateEvaluateToShop(@PathVariable String token, @RequestBody MineMallEvaluate mallEvaluate){
		
		if (mallEvaluate==null||mallEvaluate.getContent()==null||mallEvaluate.getScore()==null||mallEvaluate.getShopId()==null||mallEvaluate.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		mallEvaluate.setGoodsId(null);
		mallEvaluate.setType((byte)1);
		mallEvaluate.setStatus((byte)1);
		Result result = evaluateService.updateEvaluateToShop(token, mallEvaluate);
		
		return result;
	}
	
	//管理员隐藏某条评价
	@RequestMapping(value="/hid/evaluate/{token}/{id}")
	@ResponseBody
	public Result managerHidEvaluate(@PathVariable String token, @PathVariable Long id){
		
		if (token==null||id==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = evaluateService.updateEvaluateHid(token, id);
		
		return result;
	}
	//用户删除某条评价
	@RequestMapping(value="/delete/evaluate/{token}/{id}")
	@ResponseBody
	public Result deleteEvaluate(@PathVariable String token, @PathVariable Long id){
		
		if (token==null||id==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = evaluateService.deleteEvaluate(token, id);
		
		return result;
	}
		
	//查询某一商品的评价列表
	@RequestMapping(value="/goods/evaluate/{goodsId}/{size}/{page}")
	@ResponseBody
	public Result getGoodsEvaluate(@PathVariable Long goodsId,@PathVariable Integer size,@PathVariable Integer page){
		
		if (goodsId==null||size==null||page==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		if (size<=0) {
			size = 10;
		}
		if (page<=0) {
			page = 1;
		}

		Result result = evaluateService.getGoodsEvaluate(goodsId,size,page);
		return result;
	}
}
